package com.assignwork.service.impl;

import com.assignwork.dto.WorkArrangeMachinesSubmitDTO;
import com.assignwork.dto.WorkArrangeWorkersSubmitDTO;
import com.assignwork.entity.WorkArrangeMachines;
import com.assignwork.entity.WorkArrangeWorkers;
import com.assignwork.mapper.WorkArrangeMachinesMapper;
import com.assignwork.service.IWorkArrangeMachinesService;
import com.assignwork.vo.WorkArrangeMachinesVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.utils.Func;
import org.springblade.system.cache.DictBizCache;
import org.springblade.system.entity.DictBiz;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName WorkArrangeMachinesServiceImpl
 * @Description 配工接口实现
 * @Author dujianyu
 * @Date 2023/12/18 15:48
 */
@Service
public class WorkArrangeMachinesServiceImpl extends BaseServiceImpl<WorkArrangeMachinesMapper, WorkArrangeMachines> implements IWorkArrangeMachinesService {
    @Override
    public boolean submitWaMachines(Long arrangeId, List<WorkArrangeMachinesSubmitDTO> data, String arrangeType) {
        Set<Long> exclude = new HashSet<>();
        if (Func.isNotEmpty(data)) {
            exclude = data.stream().map(WorkArrangeMachinesSubmitDTO::getId).filter(Objects::nonNull).collect(Collectors.toSet());
        }
        boolean removeOldSuccess = removeByArrangeId(arrangeId, exclude);
        if (!Func.isNull(data) && !Func.isEmpty(data)) {
            List<WorkArrangeMachines> newMachines = new ArrayList<>();
            for (WorkArrangeMachinesSubmitDTO item : data) {
                WorkArrangeMachines newMachine = new WorkArrangeMachines();
                newMachine.setArrangeId(arrangeId);
                newMachine.setMachines(item.getMachines());
                newMachine.setTeamId(item.getTeamId());
                newMachine.setArrangeType(arrangeType);
                newMachine.setMachineKind(item.getMachineKind());
                if (item.getId() != null) {
                    newMachine.setId(item.getId());
                }
                newMachines.add(newMachine);
            }
            return removeOldSuccess && super.saveOrUpdateBatch(newMachines);
        } else {
            return removeOldSuccess;
        }
    }

    private boolean removeByArrangeId(Long arrangeId, Set<Long> exclude) {
        QueryWrapper<WorkArrangeMachines> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .select(WorkArrangeMachines::getId)
                .eq(WorkArrangeMachines::getArrangeId,arrangeId)
                .notIn(Func.isNotEmpty(exclude), WorkArrangeMachines::getId, exclude)
                .eq(WorkArrangeMachines::getIsDeleted,0);
        List<WorkArrangeMachines> entities = baseMapper.selectList(queryWrapper);
        if (!Func.isEmpty(entities)) {
            List<Long> ids = entities.stream().map(WorkArrangeMachines::getId).collect(Collectors.toList());
            return super.deleteLogic(ids);
        }
        return true;
    }

    @Override
    public List<WorkArrangeMachinesVO> infoByArrangeId(Long arrangeId) {
        if (Func.isNull(arrangeId)) {
            throw new RuntimeException("没有配工ID");
        }
        List<WorkArrangeMachinesVO> data = baseMapper.selectInfoByArrangeId(arrangeId);
        for (WorkArrangeMachinesVO vo : data) {
            vo.setMachineKindName(DictBizCache.getValue("machine_kind", vo.getMachineKind()));
        }
        return data;
    }

    @Override
    public boolean removeByArrangeId(Long arrangeId) {
        QueryWrapper<WorkArrangeMachines> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .select(WorkArrangeMachines::getId)
                .eq(WorkArrangeMachines::getArrangeId,arrangeId)
                .eq(WorkArrangeMachines::getIsDeleted,0);
        List<WorkArrangeMachines> entities = baseMapper.selectList(queryWrapper);
        if (!Func.isEmpty(entities)) {
            List<Long> ids = entities.stream().map(WorkArrangeMachines::getId).collect(Collectors.toList());
            return super.deleteLogic(ids);
        }
        return true;
    }
}
